﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class ShipEventTrigger
{
    public enum TriggerType
    {
        None,
        ControleSystem,
        PartStateChenged,
    }
    public Ship m_Ship;
    public TriggerType m_TriggerType = TriggerType.None;

    public virtual void Init()
    {
    }
    public virtual void BindShip(Ship ship)
    {
        if (ship == null)
        {
            Debug.LogWarning("ShipEventTrigger::BindShip(Ship) Cann't found ship in ShipEventTriggerr");
            return;
        }
    }
    public static string GetTriggerTypeName(TriggerType type)
    {
        string triggerName = "NONE";
        switch(type)
        {
            case TriggerType.ControleSystem:
                triggerName = "控制系统";
                break;
            case TriggerType.PartStateChenged:
                triggerName = "组件状态变化";
                break;
        }
        return triggerName;
    }
    public virtual string GetName()
    {
        return "NONE";
    }
    public virtual bool CheckTrigger()
    {
        return false;
    }
    public virtual JsonData Save()
    {
        JsonData root = new JsonData();
        root["TriggerType"] = (int)m_TriggerType;
        return root;
    }
    public virtual bool Load(JsonData root)
    {
        IDictionary<string, JsonData> rootObj = root.ValueAsObject();

        if (!rootObj.ContainsKey("TriggerType"))
            return false;
        m_TriggerType = (TriggerType)rootObj["TriggerType"].ValueAsInt();
        return true;
    }

    public static ShipEventTrigger Create(JsonData root)
    {
        IDictionary<string, JsonData> rootObj = root.ValueAsObject();

        TriggerType triggerType = TriggerType.None;
        if (rootObj.ContainsKey("TriggerType"))
            triggerType = (TriggerType)rootObj["TriggerType"].ValueAsInt();

        ShipEventTrigger trigger = null;
        switch (triggerType)
        {
            case TriggerType.ControleSystem:
                trigger = new ControleTrigger();
                break;
        }
        trigger.Load(root);
        return trigger;
    }
}
